package pw.caffe.common.net;

import java.io.UnsupportedEncodingException;

import org.apache.mina.core.buffer.IoBuffer;

public class NEXRequest {
	private Integer length;
	private String command;
	private IoBuffer ioMessage;
	

	public NEXRequest(String command, IoBuffer ioMessage) {
		this.command = command;
		this.ioMessage = ioMessage;
		this.length = ioMessage.limit();
	}

	public void setToRead() {
		this.ioMessage.position(8);
	}

	public void restore() {
		this.ioMessage.position(0);
	}

	public Integer getLength() {
		return length;
	}

	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public final byte readByte() {
		try {

			return ioMessage.get();
		} catch (Exception e) {
		}
		return 0;
	}

	public final int readInt() {
		try {
			return ioMessage.getInt();
		} catch (Exception e) {
		}
		return 0;
	}

	public final short readShort() {
		try {
			return ioMessage.getShort();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public final double readDouble() {
		try {
			return ioMessage.getDouble();
		} catch (Exception e) {
		}
		return 0;
	}

	public final float readFloat() {
		try {
			return ioMessage.getFloat();
		} catch (Exception e) {
		}
		return 0;
	}

	public final long readLong() {
		try {
			return ioMessage.getLong();
		} catch (Exception e) {
		}
		return 0;
	}

	public final byte[] readByte(short length) {
		byte[] result = new byte[length];
		try {
			ioMessage.get(result);
		} catch (Exception e) {
		}
		return result;
	}

	public final int[] readIntArray(short length) {
		int[] result = new int[length];
		try {
			for (int i = 0; i < length; i++)
				result[i] = ioMessage.getInt();
		} catch (Exception e) {
		}
		return result;
	}

	public final short[] readShortArray(short length) {
		short[] result = new short[length];
		try {
			for (int i = 0; i < length; i++)
				result[i] = ioMessage.getShort();
		} catch (Exception e) {
		}
		return result;
	}

	public final double[] readDoubleArray(short length) {
		double[] result = new double[length];
		try {
			for (int i = 0; i < length; i++)
				result[i] = ioMessage.getDouble();
		} catch (Exception e) {
		}
		return result;
	}

	public final float[] readFloatArray(short length) {
		float[] result = new float[length];
		try {
			for (int i = 0; i < length; i++)
				result[i] = ioMessage.getFloat();
		} catch (Exception e) {
		}
		return result;
	}

	public final long[] readLongArray(short length) {
		long[] result = new long[length];
		try {
			for (int i = 0; i < length; i++)
				result[i] = ioMessage.getLong();
		} catch (Exception e) {
		}
		return result;
	}

	public final String readString(short len) {
		byte[] bt = new byte[len];
		try {
			for (int i = 0; i < len; i++)
				bt[i] = ioMessage.get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		String ret="";
		try {
			ret = new String(bt,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return ret;
	}

	public final String[] readStringArray(short length) {
		String[] result = new String[length];
		try {
			for (int i = 0; i < length; i++) {
				short lenS = ioMessage.getShort();
				result[i] = readString(lenS);
			}
		} catch (Exception e) {
		}
		return result;
	}

}
